{% extends 'base.html' %}
{% load static %}

{% block title %}电影列表 - 电影院票务管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <h2 class="mb-4"><i class="fas fa-film me-2"></i>电影列表</h2>
        
        <!-- 搜索栏 -->
        <div class="card mb-4">
            <div class="card-body">
                <form method="get" class="row g-3">
                    <div class="col-md-8">
                        <input type="text" class="form-control" name="search" 
                               placeholder="搜索电影名称、导演、演员或类型..." 
                               value="{{ search|default:'' }}">
                    </div>
                    <div class="col-md-4">
                        <button type="submit" class="btn btn-primary me-2">
                            <i class="fas fa-search me-1"></i>搜索
                        </button>
                        <a href="{% url 'movie_list' %}" class="btn btn-outline-secondary">
                            <i class="fas fa-times me-1"></i>清除
                        </a>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- 搜索结果统计 -->
        {% if search %}
            <div class="alert alert-info">
                <i class="fas fa-info-circle me-1"></i>
                搜索 "{{ search }}" 的结果：共找到 {{ page_obj.paginator.count }} 部电影
            </div>
        {% endif %}
        
        <!-- 电影网格 -->
        <div class="row">
            {% for movie in page_obj %}
                <div class="col-md-6 col-lg-4 col-xl-3 mb-4">
                    <div class="card h-100 movie-card">
                        <img src="{% if movie.poster %}{{ movie.poster.url }}{% else %}{% static 'images/default-poster.jpg' %}{% endif %}" 
                             class="card-img-top" alt="{{ movie.title }}" style="height: 350px; object-fit: cover;">
                        <div class="card-body">
                            <h6 class="card-title">{{ movie.title }}</h6>
                            <p class="card-text text-muted small">
                                <i class="fas fa-user me-1"></i>{{ movie.director }}
                            </p>
                            <p class="card-text text-muted small">
                                <i class="fas fa-tag me-1"></i>{{ movie.genre }}
                            </p>
                            <p class="card-text text-muted small">
                                <i class="fas fa-clock me-1"></i>{{ movie.duration }}分钟
                            </p>
                            <div class="d-flex justify-content-between align-items-center">
                                <span class="badge bg-warning text-dark">
                                    <i class="fas fa-star me-1"></i>{{ movie.rating }}
                                </span>
                                <span class="text-primary fw-bold">¥{{ movie.price }}</span>
                            </div>
                        </div>
                        <div class="card-footer bg-transparent">
                            <div class="d-grid gap-2">
                                <a href="{% url 'movie_detail' movie.id %}" class="btn btn-primary btn-sm">
                                    <i class="fas fa-eye me-1"></i>查看详情
                                </a>
                                {% if movie.is_active %}
                                    <span class="badge bg-success">正在上映</span>
                                {% else %}
                                    <span class="badge bg-secondary">已下映</span>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                </div>
            {% empty %}
                <div class="col-12 text-center py-5">
                    <i class="fas fa-film fa-3x text-muted mb-3"></i>
                    <h4 class="text-muted">暂无电影信息</h4>
                    {% if search %}
                        <p class="text-muted">没有找到匹配的电影，请尝试其他关键词</p>
                        <a href="{% url 'movie_list' %}" class="btn btn-primary">查看所有电影</a>
                    {% endif %}
                </div>
            {% endfor %}
        </div>
        
        <!-- 分页 -->
        {% if page_obj.has_other_pages %}
            <nav aria-label="电影列表分页" class="mt-4">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?page=1{% if search %}&search={{ search }}{% endif %}">
                                <i class="fas fa-angle-double-left"></i>
                            </a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search %}&search={{ search }}{% endif %}">
                                <i class="fas fa-angle-left"></i>
                            </a>
                        </li>
                    {% endif %}
                    
                    {% for num in page_obj.paginator.page_range %}
                        {% if page_obj.number == num %}
                            <li class="page-item active">
                                <span class="page-link">{{ num }}</span>
                            </li>
                        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ num }}{% if search %}&search={{ search }}{% endif %}">{{ num }}</a>
                            </li>
                        {% endif %}
                    {% endfor %}
                    
                    {% if page_obj.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search %}&search={{ search }}{% endif %}">
                                <i class="fas fa-angle-right"></i>
                            </a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if search %}&search={{ search }}{% endif %}">
                                <i class="fas fa-angle-double-right"></i>
                            </a>
                        </li>
                    {% endif %}
                </ul>
            </nav>
        {% endif %}
    </div>
</div>
{% endblock %} 